package cn.zy.company.servlet;

import cn.zy.company.entity.News;
import cn.zy.company.entity.PageObj;
import cn.zy.company.service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/showNews")
public class ShowNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            NewsService newsService = new NewsService();
            if (id != null && id != "") {
                PageObj<News> obj = newsService.findById(Integer.valueOf(id));
                List<News> list = obj.getList();
                for (News news : list) {
                    req.setAttribute("news",news);
                    req.getRequestDispatcher("WEB-INF/jsp/showNews.jsp").forward(req, resp);
                    return;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
